package com.anuprayog.happypur;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.content.ContextWrapper;
import java.io.File;

/**
 * @author amit This activity displays the highest score achieved by the user
 */
public class HighActivity extends Activity {

	private ImageView imageview2, imageview3, imageview4;

	public int HIGH_SCORE;

	private String scoreString;

	private String filename = "MyFile.txt";
	private String filepath = "MyFileStorage";
	File myInternalFile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_high);

		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		// code added by manoj chand on 23 july 2013
		ContextWrapper contextWrapper = new ContextWrapper(
				getApplicationContext());
		File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
		myInternalFile = new File(directory, filename);
		HIGH_SCORE = getHighScore();

		scoreString = String.valueOf(HIGH_SCORE);

		// setting images for score
		imageview2 = (ImageView) findViewById(R.id.imageView2);
		imageview3 = (ImageView) findViewById(R.id.imageView3);
		imageview4 = (ImageView) findViewById(R.id.imageView4);

		// parsing score string to print score in the defined image views
		if (scoreString.charAt(0) == '0') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.zero));
		} else if (scoreString.charAt(0) == '1') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.one));
		} else if (scoreString.charAt(0) == '2') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.two));
		} else if (scoreString.charAt(0) == '3') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.three));
		} else if (scoreString.charAt(0) == '4') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.four));
		} else if (scoreString.charAt(0) == '5') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.five));
		} else if (scoreString.charAt(0) == '6') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.six));
		} else if (scoreString.charAt(0) == '7') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.seven));
		} else if (scoreString.charAt(0) == '8') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.eight));
		} else if (scoreString.charAt(0) == '9') {
			imageview2.setImageBitmap(BitmapFactory.decodeResource(
					getResources(), R.drawable.nine));
		}

		if (HIGH_SCORE > 9) {
			if (scoreString.charAt(1) == '0') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.zero));
			} else if (scoreString.charAt(1) == '1') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.one));
			} else if (scoreString.charAt(1) == '2') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.two));
			} else if (scoreString.charAt(1) == '3') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.three));
			} else if (scoreString.charAt(1) == '4') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.four));
			} else if (scoreString.charAt(1) == '5') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.five));
			} else if (scoreString.charAt(1) == '6') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.six));
			} else if (scoreString.charAt(1) == '7') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.seven));
			} else if (scoreString.charAt(1) == '8') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.eight));
			} else if (scoreString.charAt(1) == '9') {
				imageview3.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.nine));
			}
		}

		if (HIGH_SCORE > 99) {
			if (scoreString.charAt(2) == '0') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.zero));
			} else if (scoreString.charAt(2) == '1') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.one));
			} else if (scoreString.charAt(2) == '2') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.two));
			} else if (scoreString.charAt(2) == '3') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.three));
			} else if (scoreString.charAt(2) == '4') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.four));
			} else if (scoreString.charAt(2) == '5') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.five));
			} else if (scoreString.charAt(2) == '6') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.six));
			} else if (scoreString.charAt(2) == '7') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.seven));
			} else if (scoreString.charAt(2) == '8') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.eight));
			} else if (scoreString.charAt(2) == '9') {
				imageview4.setImageBitmap(BitmapFactory.decodeResource(
						getResources(), R.drawable.nine));
			}
		}

	}

	// function created by manoj chand to get high score from internal storage.
	public int getHighScore() {
		String myData = "0";
		try {
			FileInputStream fis = new FileInputStream(myInternalFile);
			DataInputStream in = new DataInputStream(fis);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				myData = strLine;
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (myData != null && myData.trim().equals(""))
			myData = "0";
		return Integer.parseInt(myData);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.high, menu);
		return true;
	}

	public void goBack(View view) {
		finish();
	}

}